package xin.codedream.codelog.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.codedream.codelog.dao.ArticleDao;
import xin.codedream.codelog.exception.ArticleException;
import xin.codedream.codelog.model.entity.Article;
import xin.codedream.codelog.model.entity.ArticleTag;
import xin.codedream.codelog.model.entity.Tag;
import xin.codedream.codelog.service.ArticleService;
import xin.codedream.codelog.service.ArticleTagsService;
import xin.codedream.codelog.service.CommentService;
import xin.codedream.codelog.service.TagsService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 文章功能服务实现
 *
 * @author NGLSL
 * @date 2018/8/19
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleDao articleDao;
    @Resource
    private ArticleTagsService articleTagsService;
    @Resource
    private TagsService tagsService;
    @Resource
    private CommentService commentService;

    @Override
    public boolean saveArticle(Article article) {
        return articleDao.insertArticle(article) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean postArticle(Article article, String tags) {
        List<Tag> tagList = tagsService.strConversionToTags(tags);
        boolean saveResult = this.saveArticle(article);
        List<ArticleTag> articleTagList = new ArrayList<>();
        articleTagList.add(new ArticleTag(article, tagList));
        boolean articleTagsResult = articleTagsService.saveArticleTags(articleTagList);
        return saveResult && articleTagsResult;
    }

    @Override
    public Article findArticleByTitle(String title) {
        return articleDao.selectArticleByTitle(title);
    }

    @Override
    public List<Article> findAllArticle(Article article) {
        return articleDao.listAllArticle(article);
    }

    @Override
    public Article findArticleByArticleId(Long articleId) {
        return articleDao.selectArticleByArticleId(articleId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateArticleByArticleId(Article article) {
        List<Tag> tagList = tagsService.strConversionToTags(article.getTags());
        article.setArticleTags(articleTagsService.findArticleTagsByArticleId(article.getArticleId()));
        boolean saveArticleTags = articleTagsService.updateArticleTag(new ArticleTag(article, tagList));
        boolean saveArticle = false;
        if (saveArticleTags) {
            saveArticle = articleDao.updateArticleByArticleId(article) > 0;
        }
        return saveArticle;
    }

    @Override
    public boolean removeArticleByArticleId(Long articleId) {
        return articleDao.deleteArticleByArticleId(articleId) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeArticleData(Article article) throws ArticleException {
        boolean removeArticleTagResult = true;
        boolean commRemoveResult = true;
        if (Objects.isNull(article)) {
            throw new ArticleException("该文章不存在");
        }
        if (Objects.nonNull(article.getComments()) && !article.getComments().isEmpty()) {
            commRemoveResult = commentService.removeCommentByArticleId(article.getArticleId());
        }
        if (Objects.nonNull(article.getArticleTags()) && !article.getArticleTags().isEmpty()) {
            removeArticleTagResult = articleTagsService.removeArticleTagByArticleId(article.getArticleId(), null);
        }
        boolean removeArticleResult = this.removeArticleByArticleId(article.getArticleId());
        if (commRemoveResult && removeArticleTagResult && removeArticleResult) {
            return true;
        } else {
            throw new ArticleException("文章删除失败文章ID：%s-文章标题：%s-删除评论结果：%s-删除标签结果：%s-删除文章结果：%s", article.getArticleId(),
                    article.getArticleTitle(), commRemoveResult, removeArticleTagResult, removeArticleResult);
        }
    }

    @Override
    public int countAllArticleQuantity() {
        return articleDao.countAllArticleQuantity();
    }

    @Override
    public List<Article> findFiveArticle() {
        return articleDao.listFiveArticle();
    }

    @Override
    public boolean updateArticleCateByCateId(Long cateId, Long newCateId) {
        return articleDao.updateArticleCateByCateId(cateId, newCateId) > 0;
    }
}
